//
//  MNDProblemViewController.m
//  minidai
//
//  Created by Vitas on 2018/7/14.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDProblemViewController.h"
#import "MNDCommonProblemModel.h"
#import "MNDCommonProblemDesCell.h"

@interface MNDProblemViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) NSMutableArray *dataContainerArray;

@end

@implementation MNDProblemViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self loadData];
    // Do any additional setup after loading the view.
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)dataContainerArray {
    if (!_dataContainerArray) {
        _dataContainerArray = [NSMutableArray array];
    }
    return _dataContainerArray;
}

- (void)loadData {
    kdeclare_weakself;
    [APIManager sendRequestWithURL:API_AUTO_PLUS_PROBLEM parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            weakSelf.dataArray = [MNDCommonProblemModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"tender"]];
            [weakSelf.tableView reloadData];
        }else {
            
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MNDCommonProblemModel *problemModel = self.dataArray[section];
    if (problemModel.isShow) {
        return 2;
    }else {
        return 1;
    }
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDCommonProblemModel *problemModel = self.dataArray[indexPath.section];
    if(indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstCell"];
        cell.textLabel.text = problemModel.question;
        return cell;
    }else {
        MNDCommonProblemDesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondCell"];
        cell.titleLabel.text = problemModel.answer;
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    MNDCommonProblemModel *problemModel = self.dataArray[indexPath.section];
    problemModel.isShow = !problemModel.isShow;
    [self.dataArray removeObjectAtIndex:indexPath.section];
    [self.dataArray insertObject:problemModel atIndex:indexPath.section];
    [self.tableView reloadData];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"contentOffset.y = %lf",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y < -60) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"scroll_up_or_down" object:@0];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
